package top.mataiji.manager.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import top.mataiji.manager.service.FileUploadService;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Auther: zhaoss
 * @Date: 2021/3/30 - 03 - 30 - 15:43
 * @Description: top.mataiji.manager.controller
 * @version: 1.0
 */
@RestController
@RequestMapping("file")
public class FileUploadController {

    @Autowired
    FileUploadService fileUploadService;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-hh");

    @RequestMapping("fileUpload")
    public String fileUpload(MultipartFile file, HttpServletRequest req) throws IOException {
        String url = null;
        /*System.out.println(file);
        System.out.println(file.getOriginalFilename());
        String originalFilename = file.getOriginalFilename();
        System.out.println(originalFilename);
        String format = sdf.format(new Date());
        //定义存放路径
        String realPsth = req.getServletContext().getRealPath("/")+format;
        File folder = new File(realPsth);
        if(!folder.exists()){
            folder.mkdir();
        }
        String newName = UUID.randomUUID().toString()+".png";
        try {
            file.transferTo(new File(folder,newName));
            url = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+"/"+format+newName;
            System.out.println(url);
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        String fileName = file.getOriginalFilename();
//        file.transferTo(new File("D:/code/shop-book",fileName));
        if(fileName.endsWith(".pdf")){

        }
        System.out.println(fileName);
        String data = sdf.format(new Date());
        fileName =
                data + System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));
        System.out.println(fileName);
        String s = fileUploadService.upLoad(file.getInputStream(), fileName);
        System.out.println(s);
        return s;
    }

}
